import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/brn/brn_bottom_button_panel.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/text_width.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/mine/user_manage/mine_add_user/views/add_user_bottom.dart';
import 'package:yt_devops/pages/mine/user_manage/mine_reset_password/views/user_change_pwd_textField.dart';
import 'package:yt_devops/pages/mine/user_manage/user_manager_user_detail/user_avatar_widget.dart';
import 'package:yt_devops/pages/site/site_setting/site_setting_base_info/widgets/item_widget.dart';
import 'package:yt_devops/pages/site/site_setting/site_setting_base_info/widgets/remark_widget.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/user_entity_manager.dart';
import 'package:yt_devops/utils/brn_row1_delegate.dart';
import 'package:yt_devops/utils/enums.dart';
import '../../../../components/log_utils.dart';
import 'mine_add_user_controller.dart';

class MineAddUserPage extends GetView<MineAddUserController> {
  const MineAddUserPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MineAddUserController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBarWidget.withRightActions(
            ResKey.mineUserManagerAddUser.tr,
          ),
          body: ListView(
            children: [
              Gaps.vGap10,
              _topWidget(),
              Gaps.vGap10,
              _middleWidget(),
              // Gaps.vGap10,
              AddUserBottom(
                  controller.pushTypeList,
                  controller.pushLevelList,
                  controller.remarkController,
                  (List list) {
                    controller.pushTypeList = list;
                    controller.checkCanCommit();
                  },

                  (List list) {
                    controller.pushLevelList = list;
                    controller.checkCanCommit();
                  }, (String ? remark)  {
                controller.remarkController.text = remark??'';
                controller.checkCanCommit();
                controller.update();
              }),
              Gaps.vGap10,
            ],
          ),
          bottomNavigationBar: Container(
            color: Colors.white,
            height: 82 + ScreenUtil().bottomBarHeight,
            padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight),
            child: YtBrnBottomButtonPanel(
                enableMainButton: controller.enableCommit,
                mainButtonName: ResKey.save.tr,
                mainButtonOnTap: () => controller.save(),
                secondaryButtonName: ResKey.cancel.tr,
                enableSecondaryButton: true,
                secondaryButtonBorder: Border.all(color: Colour.dividerColor),
                secondaryButtonOnTap: () => Get.back()),
          ),
        );
      },
    );
  }

  _topWidget() {
    return Container(
      clipBehavior: Clip.hardEdge,
      margin: const EdgeInsets.only(left: 16,right: 16),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: Colors.white,
      ),
      height: 76 + 50,
      child: Column(
        children: [
          UserAvatarWidget(
            controller.avatar,
            (avatar) {
              controller.avatar = avatar;
              controller.update();
            },
          ),
          Container(
            height: 1,
            color: Colour.dividerColor,
          ),
          ItemWidget(
            ResKey.mineUserManagerUserStatus.tr,
            controller.statusController,
            isShowRight: true,
            isCanEdit: false,
            titleColor: Colour.mainTextColor,
            titleFont: 16,
            isRequired: true,
            hintString: ResKey.pleSelect.tr,
            clickCallBack: () {
              showBottomPop(controller.statusList);
            },
          ),
        ],
      ),
    );
  }

  _middleWidget() {
    TextStyle style = ytTextStyle(
        fontSize: 16,
        fontWeight: FontWeight.normal,
        color: Colour.mainTextColor);
    num titleWidth1 =
        TextWidth.calculateTextSize(ResKey.mineUserManagerInitPwd.tr, style);
    num titleWidth2 =
        TextWidth.calculateTextSize(ResKey.mineUserManagerConfirmPwd.tr, style);
    return Container(
      margin: const EdgeInsets.only(left: 16,right: 16),
      clipBehavior: Clip.hardEdge,
      decoration: const BoxDecoration(
        borderRadius: BorderRadius.only(topLeft: Radius.circular(8),topRight: Radius.circular(8),),
        color: Colors.white,
      ),
      child: Column(
        children: [
          ItemWidget(
              ResKey.mineUserManagerUserName.tr, controller.userNameController,
              titleColor: Colour.mainTextColor,
              titleFont: 16,
              isCanEdit: true,
              isRequired: true, textChangeCallBack: (text) {
            controller.checkCanCommit();
          }),
          ItemWidget(ResKey.mineUserManagerAccountName.tr,
              controller.accountNameController,
              titleColor: Colour.mainTextColor,
              titleFont: 16,
              isCanEdit: true,
              isRequired: true, textChangeCallBack: (text) {
            controller.checkCanCommit();
          }),
          ItemWidget(
            ResKey.mineUserManagerRole.tr,
            controller.roleController,
            isShowRight: true,
            isCanEdit: false,
            isRequired: true,
            hintString: ResKey.pleSelect.tr,
            titleColor: Colour.mainTextColor,
            titleFont: 16,
            isShowDivider: false,
            clickCallBack: () {
              controller.chooseRole();
            },
          ),
          ItemWidget(
            ResKey.mineUserManagerOrganization.tr,
            controller.organizationController,
            titleColor: Colour.mainTextColor,
            titleFont: 16,
            isShowRight: controller.isShowOrganizationRight,
            isCanEdit: false,
            isRequired: true,
            hintString: ResKey.pleSelect.tr,
            clickCallBack: () {
              if (controller.isShowOrganizationRight) {
                controller.chooseOrganization();
              }
            },
          ),
          ItemWidget(
            ResKey.mineUserManagerMobile.tr,
            controller.mobileController,
            titleColor: Colour.mainTextColor,
            titleFont: 16,
            isCanEdit: true,
            isRequired: false,
            textChangeCallBack: (text) {
              controller.checkCanCommit();
            },
          ),
          ItemWidget(
            ResKey.mineUserManagerUserEmail.tr,
            controller.emailController,
            titleColor: Colour.mainTextColor,
            titleFont: 16,
            isCanEdit: true,
            isShowDivider: false,
          ),
          Row(
            children: [
              Container(
                alignment: Alignment.center,
                margin: const EdgeInsets.only(left: 6, right: 2),
                child: Text(
                  '*',
                  style: ytTextStyle(
                      color: Colour.warningTextColor,
                      fontWeight: FontWeight.w500),
                ),
              ),
              Text(
                ResKey.mineUserManagerInitPwd.tr,
                style: ytTextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.normal,
                    color: Colour.mainTextColor),
              ),
              Spacer(),
              SizedBox(
                // color: Colors.red,
                width: ScreenUtil().screenWidth - titleWidth1 - 46.w,
                child: pwdTextField(
                    controller.pwdController, ResKey.plsInput.tr, (p0) {
                  controller.checkCanCommit();
                }),
              )
            ],
          ),
          Container(
            height: 1,
            margin: EdgeInsets.only(left: 16.w, right: 16.w),
            color: Colour.dividerColor,
          ),
          Row(
            children: [
              Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(left: 6.w, right: 2.w),
                child: Text(
                  '*',
                  style: ytTextStyle(
                      color: Colour.warningTextColor,
                      fontWeight: FontWeight.w500),
                ),
              ),
              Text(
                ResKey.mineUserManagerConfirmPwd.tr,
                style: ytTextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.normal,
                    color: Colour.mainTextColor),
              ),
              Spacer(),
              SizedBox(
                // color: Colors.red,
                width: ScreenUtil().screenWidth - titleWidth2 - 46.w,
                child: pwdTextField(
                    controller.againPwdController, ResKey.plsInput.tr, (p0) {
                  controller.checkCanCommit();
                }),
              )
            ],
          ),
          Visibility(
              visible: !controller.systemBuiltRoles
                  .contains(controller.selectedRoleId),
              child: Container(
                height: 1,
                margin: EdgeInsets.only(left: 16.w, right: 16.w),
                color: Colour.dividerColor,
              )),
          Visibility(
              visible: !controller.systemBuiltRoles
                  .contains(controller.selectedRoleId),
              child: ItemWidget(
                ResKey.mineUserManagerUserRelationSite.tr,
                controller.relationSiteController,
                titleColor: Colour.mainTextColor,
                titleFont: 16,
                isCanEdit: false,
                isRequired: true,
                isShowRight: true,
                hintString: ResKey.pleSelect.tr,
                clickCallBack: () {
                  controller.chooseRelateSite();
                },
              )),
        ],
      ),
    );
  }

  _bottomWidget() {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          Container(
            height: 50,
            // color: Colors.red,
            child: Row(
              children: [
                Container(
                  alignment: Alignment.center,
                  margin: const EdgeInsets.only(left: 6, right: 2),
                  child: Text(
                    '*',
                    style: ytTextStyle(
                        color: Colour.warningTextColor,
                        fontWeight: FontWeight.w500),
                  ),
                ),
                Text(
                  '告警推送方式',
                  style: ytTextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.normal,
                      color: Colour.mainTextColor),
                ),
                Text(
                  '(可多选）',
                  style: ytTextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.normal,
                      color: Colour.lightTextColor),
                ),
              ],
            ),
          ),
          Container(
            height: 52,
            // color: Colors.green,
            child: Row(
              children: [
                Expanded(
                    child: Container(
                  height: 34,
                  decoration: BoxDecoration(
                    color: Colour.primaryColor.withOpacity(0.1),
                    borderRadius: BorderRadius.circular(4),
                  ),
                  alignment: Alignment.center,
                  margin: const EdgeInsets.only(left: 16, right: 12),
                  child: Text(
                    'APP消息推送',
                    style: ytTextStyle(
                        color: Colour.primaryColor,
                        fontWeight: FontWeight.w500),
                  ),
                )),
                Expanded(
                    child: Container(
                  height: 34,
                  decoration: BoxDecoration(
                    color: Colour.backgroundColor,
                    borderRadius: BorderRadius.circular(4),
                  ),
                  alignment: Alignment.center,
                  margin: const EdgeInsets.only(right: 16),
                  child: Text(
                    '邮箱',
                    style: ytTextStyle(
                        color: Colour.mainTextColor,
                        fontWeight: FontWeight.normal),
                  ),
                )),
              ],
            ),
          ),
          Container(
            height: 1,
            margin: EdgeInsets.only(left: 16.w, right: 16.w),
            color: Colour.dividerColor,
          ),
          Container(
              height: 50,
              child: Row(
                children: [
                  Container(
                    alignment: Alignment.center,
                    margin: EdgeInsets.only(left: 6.w, right: 2.w),
                    child: Text(
                      '*',
                      style: ytTextStyle(
                          color: Colour.warningTextColor,
                          fontWeight: FontWeight.w500),
                    ),
                  ),
                  Text(
                    '告警推送方式',
                    style: ytTextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.normal,
                        color: Colour.mainTextColor),
                  ),
                  Text(
                    '(可多选）',
                    style: ytTextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.normal,
                        color: Colour.lightTextColor),
                  ),
                ],
              )),
          Container(
            height: 52,
            // color: Colors.green,
            child: Row(
              children: [
                Expanded(
                    child: Container(
                  height: 34,
                  decoration: BoxDecoration(
                    color: Colour.primaryColor.withOpacity(0.1),
                    borderRadius: BorderRadius.circular(4),
                  ),
                  alignment: Alignment.center,
                  margin: const EdgeInsets.only(left: 16, right: 12),
                  child: Text(
                    ResKey.error.tr,
                    style: ytTextStyle(
                        color: Colour.primaryColor,
                        fontWeight: FontWeight.w500),
                  ),
                )),
                Expanded(
                    child: Container(
                  height: 34,
                  decoration: BoxDecoration(
                    color: Colour.backgroundColor,
                    borderRadius: BorderRadius.circular(4),
                  ),
                  alignment: Alignment.center,
                  margin: const EdgeInsets.only(right: 16),
                  child: Text(
                    ResKey.alarmImportant.tr,
                    style: ytTextStyle(
                        color: Colour.mainTextColor,
                        fontWeight: FontWeight.normal),
                  ),
                )),
              ],
            ),
          ),
          Container(
            height: 52,
            // color: Colors.green,
            child: Row(
              children: [
                Expanded(
                    child: Container(
                  height: 34,
                  decoration: BoxDecoration(
                    color: Colour.primaryColor.withOpacity(0.1),
                    borderRadius: BorderRadius.circular(4),
                  ),
                  alignment: Alignment.center,
                  margin: const EdgeInsets.only(left: 16, right: 12),
                  child: Text(
                    ResKey.warn.tr,
                    style: ytTextStyle(
                        color: Colour.primaryColor,
                        fontWeight: FontWeight.w500),
                  ),
                )),
                Expanded(
                    child: Container(
                  height: 34,
                  decoration: BoxDecoration(
                    color: Colour.backgroundColor,
                    borderRadius: BorderRadius.circular(4),
                  ),
                  alignment: Alignment.center,
                  margin: const EdgeInsets.only(right: 16),
                  child: Text(
                    ResKey.info.tr,
                    style: ytTextStyle(
                        color: Colour.mainTextColor,
                        fontWeight: FontWeight.normal),
                  ),
                )),
              ],
            ),
          ),
          // Gaps.vGap20,
          RemarkWidget(
            controller.remarkController,
            (str) {
              controller.update();
            },
            isCanEdit: true,
            isUser: true,
          ),
        ],
      ),
    );
  }

  ///弹出底部弹窗
  showBottomPop(List<String> list) {
    BrnMultiDataPicker(
      context: Get.context!,
      title: ResKey.mineUserManagerUserStatus.tr,
      confirmClick: (indexList) {
        controller.statusController.text = list[indexList[0]].toString();
        controller.selectedStatusIndex = indexList[0];
        controller.checkCanCommit();
      },
      delegate: Brn1RowDelegate(list,
          firstSelectedIndex: controller.selectedStatusIndex),
    ).show();
  }
}
